package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.SysTrRecordMapper;
import com.so206.po.SysTrRecord;
import com.so206.service.SysTranssferAccountService;
import com.so206.utils.BeanCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysTranssferAccountServiceImpl implements SysTranssferAccountService {

    @Autowired
    private SysTrRecordMapper mapper;

    @Override
    public PageInfo<SysTrRecord> find_by_page(Integer page, Integer rows, Integer status, Long src_pid, Long dst_pid, String to_context) {
        PageHelper.startPage(page, rows);
        Long pid = 0L;
        List<SysTrRecord> records = mapper.selectByParentIdTContextDstIdSrcIdStatus(status, src_pid, dst_pid, to_context, pid,null);
        return new PageInfo<>(records);
    }

    @Override
    public Long save_record(SysTrRecord record) {
        record.setCreateTime(new Date());
        mapper.insert(record);
        return record.getId();
    }

    @Override
    public SysTrRecord find_by_id(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void updat_record(SysTrRecord record) {
        SysTrRecord model = mapper.selectByPrimaryKey(record.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(record, model);
            mapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public void delete_by_id(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public SysTrRecord find_by_parent_id(Long pid) {
        List<SysTrRecord> list = mapper.selectByParentIdTContextDstIdSrcIdStatus(null, null, null, null, pid,null);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public PageInfo<SysTrRecord> find_by_page2(Integer page, Integer rows, Integer status, Long opt_id, Long dst_pid, String to_context) {
        PageHelper.startPage(page, rows);
        List<SysTrRecord> records = mapper.selectByParentIdTContextDstIdSrcIdStatus(status,null,dst_pid,to_context,null,opt_id);
        return new PageInfo<>(records);
    }


}
